// axios基础封装
import axios from 'axios'

export const request: any = axios.create({
  baseURL: 'http://localhost:8080',
  timeout: 5000,
})

// 请求拦截器
request.interceptors.request.use(
  (config: any) => {
    // 请求拦截器
    return config
  },
  (error: any) => {
    return Promise.reject(error)
  },
)

// 响应拦截器
request.interceptors.response.use(
  (res: any) => {
    return res.data
  },
  (error: any) => {
    //处理网络错误
    let msg = ''
    const status = error.response.status
    switch (status) {
      case 401:
        msg = 'token过期'
        break
      case 403:
        msg = '无权访问'
        break
      case 404:
        msg = '请求地址错误'
        break
      case 500:
        msg = '服务器出现问题'
        break
      default:
        msg = '网络出现问题'
        break
    }
    return Promise.reject(error)
  },
)
